<?php

/**
 * This is the model class for table "lookup".
 */
Yii::import('Core.models.base.LookupBase');
class Lookup extends LookupBase
{
    private static $_items=array();
    
     /**
    * list all types of Lookup table, used for workflow: Setting > Lookup
    * 
    */
    public static function types()
    {
        $data=array();
        $models= self::model()->findAll(array('select'=>'type','distinct'=>true,'order'=>'type ASC'));
        if(!$models) return array();
        foreach($models as $model){
            $data[$model->type]=$model->type;
        }
        return $data;
    }
    
     /**
    * Get the first type of types list(order by type asc)
    * 
    */
    
    public static function firstType(){
        $model=self::model()->find(array('select'=>'type','distinct'=>true,'order'=>'type ASC'));
        if(!$model) return null;
        //return type 
        return $model->type;
    }
 
    /**
    * Get an array of code => name of a type that can be used for populating a drop-down list
    * 
    * @param mixed $type
    * @return Lookup
    */
    
    
    public static function items($type)
    {
        if(!isset(self::$_items[$type]))
            self::loadItems($type);
        return self::$_items[$type];
    }
 
    /**
    * Get name of a code
    * 
    * @param mixed $type
    * @param mixed $code
    * @return Lookup
    */
    public static function item($type,$code)
    {
        if(!isset(self::$_items[$type]))
            self::loadItems($type);
        return isset(self::$_items[$type][$code]) ? self::$_items[$type][$code] : false;
    }
 
    private static function loadItems($type)
    {
        self::$_items[$type]=array();
        $models=self::model()->findAll(array(
            'condition'=>'type=:type',
            'params'=>array(':type'=>$type),
            'order'=>'position',
        ));
        foreach($models as $model)
            self::$_items[$type][$model->code]=$model->name;
    }
    
    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('name, code, type', 'required'),
             array('type', 'varible'),
            array('position', 'numerical', 'integerOnly'=>true),
            array('name, type', 'length', 'max'=>128),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, name, code, type, position', 'safe', 'on'=>'search'),
        );
    }
    /**
    * Add validation rule for type, type name must be a valid name.(begin with an alphabet)
    * 
    * @param mixed $attribute
    * @param mixed $params
    */
    public function varible($attribute,$params)
    {
        $pattern = '/^[A-Za-z]/';  
        if(!preg_match($pattern, $this->$attribute))
          $this->addError($attribute, 'Type must begin with an alphabet character!');
    }
    
    
    public function search()
    {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria=new CDbCriteria;

        $criteria->compare('id',$this->id);
        $criteria->compare('name',$this->name,true);
        $criteria->compare('code',$this->code,true);
        if($this->type==null)
            $this->type=Lookup::firstType();
        $criteria->compare('type',$this->type,true);
        $criteria->compare('position',$this->position);
        $criteria->order='position ASC';
        return new CActiveDataProvider(get_class($this), array(
            'criteria'=>$criteria,
        ));
    }
}